package cc.wsyw126.java.lambda.java8lambda.exercise;

import cc.wsyw126.java.lambda.java8lambda.base.Artist;

import java.util.Iterator;
import java.util.List;


/**
 * �����ļ�����ʽ�ͱ仯
 * 
 * @author liuning
 *
 */
public class Iteration {

	/**
	 * @param allArtists
	 * @return
	 */
	public int externalCountArtistsFromUK(List<Artist> allArtists) {

		int count = 0;
		for (Artist artist : allArtists) {
			if (artist.isFrom("UK")) {
				count++;
			}
		}

		return count;
	}

	public int externalCountArtistsFromLondonExpanded(List<Artist> allArtists) {

		int count = 0;
		Iterator<Artist> iterator = allArtists.iterator();
		while (iterator.hasNext()) {
			Artist artist = iterator.next();
			if (artist.isFrom("UK")) {
				count++;
			}
		}

		return count;
	}

	public long internalCountArtistsFromLondon(List<Artist> allArtists) {

		long count = allArtists.stream().filter(artist -> artist.isFrom("UK")).count();

		return count;
	}

	public void filterArtistsFromLondon(List<Artist> allArtists) {
		// BEGIN filter_londoners
		allArtists.stream().filter(artist -> artist.isFrom("London"));
		// END filter_londoners
	}

	public void filterArtistsFromLondonPrinted(List<Artist> allArtists) {
		// BEGIN filter_londoners_printed
		allArtists.stream().filter(artist -> {
			System.out.println(artist.getName());
			return artist.isFrom("London");
		});
		// END filter_londoners_printed
	}

	public long internalCountArtistsFromLondonPrinted(List<Artist> allArtists) {
		// BEGIN internal_count_londoners_printed
		long count = allArtists.stream().filter(artist -> {
			System.out.println(artist.getName());
			return artist.isFrom("London");
		}).count();
		// END internal_count_londoners_printed
		return count;
	}

}
